import cv2  # ??OpenCV?
import numpy as np  # ??numpy??
import time  # ??time??
import socket
import sys
                                                                                                                                                                                     
def init_cam():
    cap = cv2.VideoCapture(-1)  # ????????0?????????Linux????-1)
    return cap

def send_data(udp_socket,frame):
    img_encode = cv2.imencode ('.jpg',frame)[1]
    data_encode = np.array(img_encode)
    str_encode = data_encode.tostring()
    udp_socket.sendto(str_encode,("192.168.1.130",8150))
    
    
def main():
    cap = init_cam()    
    flag = True
    udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    while flag:
        print(1)
        if cap.isOpened():  # ?????????
            flag, frame = cap.read()  # ?????????????flag?frame
        else:
            print("??????")
            flag = False  # ???flag?False????while???
        print(frame.shape)
        cv2.imshow("frame", frame)
        
        frame = cv2.resize(frame,(240,320),interpolation=cv2.INTER_AREA)
        
        send_data(udp_socket,frame)
        #udp_socket.sendto(b"hahahaha",("192.168.1.130",8120))
